/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

function isblank(tmpstring)
	{
	tmpstring=trim(tmpstring);
	if (tmpstring.length==0)return true;
	else return false;
}

function remove_quotes(tmpstring)
	{
	while (tmpstring.indexOf("\'")!=-1)
		{
		tmpstring = tmpstring.substring(0,tmpstring.indexOf("\'"))+tmpstring.substring(tmpstring.indexOf("\'")+1,tmpstring.length);
	}
	while (tmpstring.indexOf("\"")!=-1)
		{
		tmpstring = tmpstring.substring(0,tmpstring.indexOf("\""))+tmpstring.substring(tmpstring.indexOf("\"")+1,tmpstring.length);
	}			
	return tmpstring;
} 

function isnumber(tmpstring)
	{
	for (x=0;x<tmpstring.length;x++)
		{
		if ((tmpstring.charCodeAt(x) < 48 || tmpstring.charCodeAt(x) > 57) && tmpstring.charCodeAt(x)!=46) return false;
	}
	return true;
}

function isdate(tmpstring) {
    //check length should be 10
    if (tmpstring.length<10) return false;

    if (tmpstring.substring(4,5)!="-" || tmpstring.substring(7,8)!="-") return false;
    if (!isnumber(tmpstring.substring(0,4)) || !isnumber(tmpstring.substring(5,7)) || !isnumber(tmpstring.substring(8,10))) return false;
    if (parseInt(tmpstring.substring(5,7),10)>12 || parseInt(tmpstring.substring(5,7),10)==0) return false;
    if (parseInt(tmpstring.substring(8,10))>31 || parseInt(tmpstring.substring(8,10),10)==0) return false;
    return true;
}

function isover18 (tmpstring) {
    var curDate = new Date();
    dt=parseInt(tmpstring.substring(8,10),10);
    mn=parseInt(tmpstring.substring(5,7),10);
    yr=parseInt(tmpstring.substring(0,4),10);

    sys_dt = curDate.getDate();
    sys_mn = curDate.getMonth()+1;
    sys_yr = curDate.getYear();
    
    // eg yr entered is 2000 and sys yr is 2005 (2000>2005-18) that means the date is over 18
    if (yr>sys_yr-18) return false;
    if (yr<sys_yr-18) return true;

    // yr is obviously the same as the sys_yr so we check for month
    if (mn>sys_mn) return false;
    if (mn<sys_mn) return true;

    // yr and mn are the same so we check for date

    if (dt>sys_dt) return false;
    
    // date is less than or equal to 18 yrs so it must be true.
    return true;
    
}
